package com.bookstore.mapper;

import com.bookstore.domain.Book;
import com.bookstore.domain.BookType;
import com.bookstore.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {
    //查询所有书籍
    @Select("<script>" +
            "select * from book" +
            "<where>" +
            "<if test=\"btype!=0\">btype=#{btype}</if>" +
            "<if test=\"bname!=null\"> And bookName like concat (\'%\',#{bname},\'%\')</if>" +
            "</where>" +
            "</script>")
    @Results({ //多表查询
            @Result(property = "bid", column = "bid", id = true),
            @Result(property = "uid", column = "uid"),
            @Result(property = "bookName", column = "bookName"),
            @Result(property = "btype",
                    javaType = BookType.class,
                    column = "btype",
                    one = @One(select = "com.bookstore.mapper.BookTypeMapper.findById")
            ),
            @Result(property = "price", column = "price"),
            @Result(property = "publishing", column = "publishing"),
            @Result(property = "bigImg", column = "bigImg"),
            @Result(property = "inventory", column = "inventory"),
            @Result(property = "author", column = "author"),
            @Result(property = "content", column = "content"),
            @Result(property = "pubTime", column = "pubTime")
    })
    public List<Book> findAll(@Param("btype") Integer btype,@Param("bname") String bname);

    //添加书籍
    @Insert("Insert into book values(null,#{uid},#{bookName},#{btype.btype},#{price},#{publishing},#{bigImg},#{inventory},#{author},#{content},#{pubTime})")
    public Integer saveBook(Book book);

    /*
           根据所属用户来查询
           管理员有权限查看所有订单信息
     */
    @Select("<script>" +
            "SELECT b.bid,b.uid,b.bookName,b.price,b.publishing,b.inventory,b.author,b.content,b.pubTime,t.tName" +
            " FROM book b,book_type t,book_user u" +
            "<where>" +
            " b.btype = t.btype AND b.btype=t.btype AND b.uid=u.uid" +
            "<if test=\"uType!=2\"> and uType=#{uType}</if>" +
            "<if test=\"btype!=0\"> and b.btype=#{btype}</if>" +
            "<if test=\"bname!=null\"> And bookName like concat (\'%\',#{bname},\'%\')</if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "bid", column = "bid", id = true),
            @Result(property = "uid", column = "uid"),
            @Result(property = "bookName", column = "bookName"),
            @Result(property = "btype.tName", column = "tName"),
            @Result(property = "price", column = "price"),
            @Result(property = "publishing", column = "publishing"),
            @Result(property = "inventory", column = "inventory"),
            @Result(property = "author", column = "author"),
            @Result(property = "pubTime", column = "pubTime")
    })
    List<Book> findByUtype(@Param("uType") Integer uType,@Param("btype") Integer btype,@Param("bname") String bname);

    @Select("select *,t.tName from book b,book_type t where b.btype=t.btype and bid=#{bid}")
    @Results({
            @Result(property = "bid", column = "bid", id = true),
            @Result(property = "uid", column = "uid"),
            @Result(property = "bookName", column = "bookName"),
            @Result(property = "btype.btype", column = "btype"),
            @Result(property = "btype.tName", column = "tName"),
            @Result(property = "price", column = "price"),
            @Result(property = "publishing", column = "publishing"),
            @Result(property = "inventory", column = "inventory"),
            @Result(property = "author", column = "author"),
            @Result(property = "pubTime", column = "pubTime")
    })
    Book findById(Integer bid);

    /*
           实现修改的Sql语句
     */
    @Update("<script>" +
            "update book" +
            "<set>" +
            "<if test=\"bookName!=null\">bookName=#{bookName},</if>" +
            "<if test=\"price!=0\">price=#{price},</if>" +
            "<if test=\"publishing!=null\">publishing=#{publishing},</if>" +
            "<if test=\"inventory!=0\">inventory=#{inventory},</if>" +
            "<if test=\"Author!=null\">Author=#{Author},</if>" +
            "<if test=\"content!=null\">content=#{content},</if>" +
            "<if test=\"pubTime!=null\">pubTime=#{pubTime},</if>" +
            "<if test=\"btype.btype!=0\">btype=#{btype.btype} </if>" +
            "</set>" +
            "where bid=#{bid}" +
            "</script>")
    int updateBook(Book book);

    @Delete("delete from book where bid=#{bid}")
    int deleteByBid(@Param("bid") Integer bid);
}
